//
//  SearchTabView.m
//  danpin
//
//  Created by chuangjia on 27/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "SearchTabView.h"
#import "CellConfig.h"
#import "ZToolsView.h"
@interface SearchTabView ()<UIWebViewDelegate>
{
    
}
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)NSString *url;
@property(nonatomic,strong)NSString *htmlData;
@property(nonatomic,strong)NSString *key;
@property(nonatomic,strong)void(^actionBlock)(id,id,id);
@property(nonatomic,strong)UIActivityIndicatorView *activityIndicator;
@property(nonatomic,strong)ZToolsView *toolsView;
@end
@implementation SearchTabView
- (id) initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor=[UIColor whiteColor];
        [self initUI];
    }
    return self;
}
- (void)dealloc
{
    [_activityIndicator stopAnimating];
    _activityIndicator=nil;
    
    if (self.webView.loading)
        [self.webView stopLoading];
    self.webView.delegate=nil;
    self.webView=nil;
    self.actionBlock=nil;
    if (_toolsView) {
        [_toolsView removeFromSuperview];
        _toolsView=nil;
    }
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)setSelectedCallBack:(void (^)(id, id,id))callback
{
    self.actionBlock=callback;
}

-(void)initUI
{
    if(_webView)
    {
        [_webView removeFromSuperview];
        _webView=nil;
    }
    //Create the web view
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-hightAuto(45))];
    self.webView.delegate = self;
    self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.scalesPageToFit = YES;
    self.webView.contentMode = UIViewContentModeRedraw;
    self.webView.opaque = YES;
    [self addSubview:self.webView];
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:CGPointMake(self.center.x, self.frame.size.height/2)];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self addSubview:_activityIndicator];
    if (!_toolsView) {
        _toolsView=[[ZToolsView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-hightAuto(45), self.bounds.size.width, hightAuto(45))];
    }
    __weak typeof (self) wSelf = self;
    [_toolsView setCallBack:^(ToolEventType type , UIView *v, id data, id key) {
        [wSelf callback:type];
    }];
    _toolsView.qgdp.hidden=YES;
    _toolsView.newdp.hidden=NO;
    [self addSubview:_toolsView];
}
- (void)loadRequestUrl:(NSString *)url key:(NSString *)key
{
    self.key = key;
    if (self.url == url)
        return;
    if (self.webView.loading)
        [self.webView stopLoading];
    
    NSURL *url1 =[NSURL URLWithString:url];
    NSURLRequest *request =[NSURLRequest requestWithURL:url1];
    [self.webView loadRequest:request];
}
#pragma mark -
#pragma mark WebView Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activityIndicator stopAnimating];
    NSString *lJs = @"document.documentElement.innerHTML";
    self.htmlData = [webView stringByEvaluatingJavaScriptFromString:lJs];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
     [_activityIndicator stopAnimating];
}

-(void)callback:(ToolEventType)type
{
    switch (type) {
        case ToolEventType_back:
        {
            if ([self.webView canGoBack]) {
                [self.webView goBack];
            }else{
                if (self.actionBlock) {
                    self.actionBlock(self.key, self.htmlData,@"back");
                }
            }
            
        }
            break;
        case ToolEventType_refresh:
        {
            if (self.webView) {
                [self.webView reload];
            }
        }
            break;
        case ToolEventType_newdp:
        {
            if (self.actionBlock) {
                self.actionBlock(self.key, self.htmlData,@"recommend");
            }
        }
            break;
     
        default:
            break;
    }
}
@end
